'''
Created on Nov 19, 2010

@author: namikata
'''

import pygame
from pygame.locals import *
from gameobjects.vector2 import Vector2
from bk.pygame.ships.game.base.GameEntity import *
from bk.pygame.ships.game.Armory import Armory
from bk.pygame.ships.game.base.Mouse import *

class Player(GameEntity):
    '''
    classdocs
    '''

    location = None
    speed = None
    max_speed = None
    accel = None
    movement_type = None
    cursor = None
    armor = None
    armor_max = None

    def __init__(self,world,name,image,objclass, objsclass, location):
        '''
        Constructor
        '''
        GameEntity.__init__(self, world, name, image, objclass, objsclass)
        
        #actual position
        self.location = Vector2(*location)
        self.speed = 150.
        self.max_speed = 150.
        self.accel = 75.
        self.movement_type = KMOV_DIRECTION
        self.cursor = None
        self.armor = 1000.
        self.armor_max = 1000.
                
    def update(self,elapsed, keyboard, mouse):
        #update direction    
        if keyboard.get_pressed_key(K_w):
            self.direction.y = -1 #going up
        elif keyboard.get_pressed_key(K_s):
            self.direction.y = 1 #going down
        else:
            self.direction.y = 0
                
        if keyboard.get_pressed_key(K_d):
            self.direction.x = 1
        elif keyboard.get_pressed_key(K_a):
            self.direction.x = -1
        else:
            self.direction.x = 0
        
        self.armory.update(float(elapsed), mouse.get_pressed(LeftButton), mouse.get_pressed(RightButton), mouse.get_pressed(CenterButton))
        
        if self.cursor:
            self.rotation = get_angle(self.location.copy(), self.cursor.location.copy())
        
        #manage weapon slots
        
        if keyboard.get_pressed_key(K_q) == 1:
            self.armory.switch_weapon()
        
        new_slot = None
        if keyboard.get_pressed_key(K_1) == 1:
            new_slot = "w1"
        if keyboard.get_pressed_key(K_2) == 1:
            new_slot = "w2"
        if keyboard.get_pressed_key(K_3) == 1:
            new_slot = "w3"
        if keyboard.get_pressed_key(K_4) == 1:
            new_slot = "w4"
        if keyboard.get_pressed_key(K_5) == 1:
            new_slot = "w5"
        if keyboard.get_pressed_key(K_6) == 1:
            new_slot = "w6"
        if keyboard.get_pressed_key(K_7) == 1:
            new_slot = "w7"
        if keyboard.get_pressed_key(K_8) == 1:
            new_slot = "w8"
        if keyboard.get_pressed_key(K_9) == 1:
            new_slot = "w9"
        if keyboard.get_pressed_key(K_0) == 1:
            new_slot = "w0"
        if mouse.wheel_down or mouse.wheel_up:
            new_slot = int(self.armory.weapon_slot[1])
            new_slot += mouse.wheel_down
            if new_slot > 9: new_slot -= 10
            new_slot -= mouse.wheel_up
            if new_slot < 0: new_slot += 10
            new_slot = "w%d" % (new_slot)
        
        if new_slot and self.armory.weapon_slot != new_slot:
            self.armory.set_weapon(new_slot)
            
        GameEntity.update(self, elapsed)
        
    def load_armory(self, weapons_file):
        self.armory = Armory(self,weapons_file)